/*
上传 api 接口
*/

package v1

import (
    "net/http"
    "strconv"

    "gitee.com/keeving/quanta-test-cat/middleware"
    "gitee.com/keeving/quanta-test-cat/model"
    "gitee.com/keeving/quanta-test-cat/utils"
    "gitee.com/keeving/quanta-test-cat/utils/errmsg"
    "gitee.com/keeving/quanta-test-cat/utils/validator"
    "github.com/gin-gonic/gin"
)

// 上传用户头像
func UploadAvatar(c *gin.Context) {
    token := c.GetHeader("Authorization")
    _, fileHeader, _ := c.Request.FormFile("file")
    username := c.PostForm("username")
    err := model.CheckUser(username)
    username2, _ := middleware.GetUserNameFromToken(token)

    fileType := fileHeader.Header.Get("Content-Type")
    isImg := validator.IsImage(fileType)

    if err != errmsg.ERROR_USERNAME_USED && isImg {
        url := model.UploadAvatar(c, username2, fileHeader, utils.Storage)

        c.JSON(http.StatusOK, gin.H{
            "code": 200,
            "msg":  errmsg.GetErrMsg(200),
            "url":  url,
        })
    } else {
        c.JSON(http.StatusOK, gin.H{
            "code": 500,
            "msg":  errmsg.GetErrMsg(500),
        })
    }
}

func UploadFile(c *gin.Context) string {
    _, fileHeader, _ := c.Request.FormFile("file")
    if fileHeader == nil {
        return ""
    }
    fileType := fileHeader.Header.Get("Content-Type")
    isImg := validator.IsImage(fileType)
    if isImg {
        url := model.UploadFile(c, fileHeader, utils.Storage)
        return url
    } else {
        return ""
    }
}

// 上下两函数相同

func UploadFiles(c *gin.Context) string {
    _, fileHeader, _ := c.Request.FormFile("files")
    if fileHeader == nil {
        return ""
    }
    fileType := fileHeader.Header.Get("Content-Type")
    isImg := validator.IsImage(fileType)
    if isImg {
        url := model.UploadFile(c, fileHeader, utils.Storage)
        return url
    } else {
        return ""
    }
}

// 上传单张猫猫图片
func UploadCat(c *gin.Context) {
    _, fileHeader, _ := c.Request.FormFile("file")
    fileType := fileHeader.Header.Get("Content-Type")
    catid := c.PostForm("kid")

    isImg := validator.IsImage(fileType)

    // if err == 200 && isImg {
    if isImg {
        model.UploadCat(c, catid, fileHeader, utils.Storage)

        c.JSON(http.StatusOK, gin.H{
            "code":  200,
            "msg": errmsg.GetErrMsg(200),
            "data": nil,
        })
    } else {
        c.JSON(http.StatusOK, gin.H{
            "code":  500,
            "msg": errmsg.GetErrMsg(500),
        })
    }
}

// 上传猫猫相册
func UploadFileCat(c *gin.Context) {
    form, _ := c.MultipartForm()
    id := c.PostForm("kid")
    files := form.File["file"]
    if len(files) == 0 {
        c.JSON(200, gin.H{
            
        })
    }

    urls := make([]string, 0)
    var ImgNumber = len(files)

    for _, fileHeader := range files {
        fileType := fileHeader.Header.Get("Content-Type")
        isImg := validator.IsImage(fileType)
        if isImg {
            url := model.UploadFile(c, fileHeader, utils.Storage)
            urls = append(urls, url)
        }
    }

    catid, _ := strconv.Atoi(id)
    err := model.UploadCatPhoto(catid, urls, ImgNumber)

    if err == 200 {
        c.JSON(http.StatusOK, gin.H{
            "code":  200,
            "msg": errmsg.GetErrMsg(200),
            "data": nil,
        })
    } else {
        c.JSON(http.StatusOK, gin.H{
            "code":  500,
            "msg": errmsg.GetErrMsg(500),
        })
    }
}
